<?php
/**
 * Creater: 李青林
 * Date Time : 2018/10/24 15:11
 */

namespace App\Http\Models;


use function GuzzleHttp\Psr7\str;

class StatisticsGoodsInfo extends BaseModel
{
    protected $table = 'statistics_goods_info';

    /**
     * lql
     * 更新统计商品数据,审核时候用
     * @param int $gid //商品id
     * @param $type // 0平推单，1爆款单，2秒杀单，3 纠错纠错统计
     * @param int $change_type //被改变的状态
     * @param int $operation_type //0,接口修改  1,后台修改 2,采集器
     * @param int $create_at //商品或者纠错的创建时间
     * @param int $old_type //原来的状态
     * @param bool $is_no_error //是否在就错状态下修改 只有在纠错状态下传递false就可
     * @param int $num //减少的数量
     */
    public static function updateStatisticsGoods($gid, $type, $change_type, $operation_type, $create_at = '', $old_type = -1, $is_no_error = true, $num = 1)
    {
        $model = self::lastModel($type);
        if (!$create_at) {
            $create_at = date('Y-m-d H:i:s');
        }
        if ($type < 3) {
            if ($change_type == 0) { //提交
                $model->increment('all_unaudited');
                $model->increment('today_unaudited');
            } elseif ($change_type == 2) { //审核通过
                $model->increment('online');
                if ($is_no_error) {
                    $model->increment('today_adopt');
                    $model->decrement('all_unaudited');
                    if (date('Y-m-d') == date('Y-m-d', strtotime($create_at))) {
                        $model->decrement('today_unaudited');
                    }
                } else {
                    if ($old_type == 9) {
                        $model->increment('today_adopt'); //纠错审核通过同样+1
                    }
                }
            } elseif (in_array($change_type, [3, 5])) { //撤销 ,拒绝
                $model->decrement('all_unaudited');
                if ($change_type == 3) {
                    $model->increment('today_refuse');
                }
                if (date('Y-m-d') == date('Y-m-d', strtotime($create_at))) {
                    $model->decrement('today_unaudited');
                }
            } elseif (in_array($change_type, [4, 6, 7, 8, 9, 10, 11])) { //下架
                $model->decrement('online');
            }
        } else {  //处理纠错的数据
            if ($change_type == 1) {
                $model->increment('today_adopt', $num);
                if (date('Y-m-d') == date('Y-m-d', strtotime($create_at))) {
                    $model->decrement('today_unaudited');
                }
                $model->decrement('all_unaudited', $num);
            } elseif ($change_type == 0) {
                $model->increment('today_unaudited', $num);
                $model->increment('all_unaudited', $num);
            }
        }
        $bool = $model->save();
        if ($bool) {
            $bool = StatisticsGoodsInfoLog::saveGoodsInfoLog($change_type, $type, $gid, $operation_type, $model->toArray(), $old_type);
        }
        return $bool;
    }

    protected $guarded = [];

    /**
     * lql
     * 返回最后的一条记录的model
     * @param $type // 0平推单，1爆款单，2秒杀单，3 纠错纠错统计
     * @return mixed
     */
    public static function lastModel($type)
    {
        $model = self::where(['date' => date('Ymd'), 'type' => $type])->first();
        if (!$model) {
            $info = self::where(['type' => $type])->orderBy('date', 'desc')->first();//获取最新数据
            //return $info;
            $data = [
                'date' => date('Ymd'),
                'type' => $type,
            ];
            if ($info) {
                $data['online'] = $info->online;
                $data['all_unaudited'] = $info->all_unaudited;
                $data['today_unaudited'] = 0;
                $data['today_adopt'] = 0;
                $data['today_refuse'] = 0;
            }
            self::create($data);
            $model = self::where(['date' => date('Ymd'), 'type' => $type])->first();
            return $model;
            //没有一条数据,更新所有数据
            /*  else {
                $info = self::getGoodsInfo($type);
                $data['online'] = $info['online'];
                $data['all_unaudited'] = $info['all_unaudited'];
                $data['today_unaudited'] = $info['today_unaudited'];
                $data['today_adopt'] = $info['today_adopt'];
                $data['today_refuse'] = $info['today_refuse'];
            }
            */
        }
        return $model;
    }

    /**
     * lql
     * 初始化数据获取
     * @param $type // 0平推单，1爆款单，2秒杀单，3 纠错纠错统计
     * @return array
     */
    public static function getGoodsInfo($type)
    {
        if ($type < 3) {
            $online = Goods::where(['goodstype' => $type, 'goodsstate' => 2])->count();
            $all_unaudited = Goods::where(['goodstype' => $type, 'goodsstate' => 0])->count();
            $today_unaudited = Goods::where(['goodstype' => $type, 'goodsstate' => 0])->whereDate('created_at', date('Y-m-d'))->count();
            $today_adopt = Goods::where(['goodstype' => $type, 'goodsstate' => 2])->whereDate('status_time', date('Y-m-d'))->count();
            $today_refuse = Goods::where(['goodstype' => $type, 'goodsstate' => 3])->whereDate('status_time', date('Y-m-d'))->count();
        } else {
            $online = 0;
            $all_unaudited = GoodsCorrection::where(['state' => 0])->count();
            $today_unaudited = GoodsCorrection::where(['state' => 0])->whereDate('created_at', date('Y-m-d'))->count();
            $today_adopt = GoodsCorrection::where(['state' => 1])->whereDate('updated_at', date('Y-m-d'))->count();
            $today_refuse = 0;
        }
        return [
            'online' => $online,
            'all_unaudited' => $all_unaudited,
            'today_unaudited' => $today_unaudited,
            'today_adopt' => $today_adopt,
            'today_refuse' => $today_refuse,
        ];
    }

    /**
     * 返回当日所有的未审核的信息
     * @return array
     */
    public static function getGoodsAllData()
    {
        $number = self::where(['date' => date('Ymd')])->count();
        if ($number != 4) {
            self::lastModel(0);
            self::lastModel(1);
            self::lastModel(2);
            self::lastModel(3);
        }
        $result = self::select('type', 'online', 'all_unaudited', 'today_unaudited', 'today_adopt')
            ->where('date', date('Ymd'))
            ->get();
        $data = [
            'ordinary_all_unaudited' => 0,
            'bursting_all_unaudited' => 0,
            'kill_all_unaudited' => 0,
            'error_all_unaudited' => 0,
        ];
        foreach ($result as $value) {
            $value->type == 0 && $data['ordinary_all_unaudited'] = $value->all_unaudited;
            $value->type == 1 && $data['bursting_all_unaudited'] = $value->all_unaudited;
            $value->type == 2 && $data['kill_all_unaudited'] = $value->all_unaudited;
            $value->type == 3 && $data['error_all_unaudited'] = $value->all_unaudited;
        }
        return $data;
    }

    /**
     * lql
     * 返回最后的一条记录的model
     * @param $type // 0平推单，1爆款单，2秒杀单，3 纠错纠错统计
     * @return mixed
     */
    public static function lastModelStart($type)
    {
        $model = self::where(['date' => date('Ymd'), 'type' => $type])->first();
        if (!$model) {
            //$info = self::where(['date' => date('Ymd', time() - 3600), 'type' => $type])->orderBy('date','desc')->first();
            $info = self::where(['type' => $type])->orderBy('date', 'desc')->first();
            $data = [
                'date' => date('Ymd'),
                'type' => $type,
            ];
            if ($info) {
                $data['online'] = $info->online;
                $data['all_unaudited'] = $info->all_unaudited;
                $data['today_unaudited'] = 0;
                $data['today_adopt'] = 0;
                $data['today_refuse'] = 0;
            } else {
                $info = self::getGoodsInfo($type);
                $data['online'] = $info['online'];
                $data['all_unaudited'] = $info['all_unaudited'];
                $data['today_unaudited'] = $info['today_unaudited'];
                $data['today_adopt'] = $info['today_adopt'];
                $data['today_refuse'] = $info['today_refuse'];
            }
            self::create($data);
            $model = self::where(['date' => date('Ymd'), 'type' => $type])->first();
        }
        return $model;
    }


}